﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using DbMapping.Common;
using VideoCache.Security;
using VideoCache.Server.BLL;
using VideoCache.Server.Model;
using VideoCache.Server.MVCWeb.Areas.Api.Models;
using VideoCache.Spider;
using VideoCache.Spider.FileFetchers;
using VideoCache.Server.MVCWeb.Models;
using System.Text.RegularExpressions;
using Fsb.Utility;

namespace VideoCache.Server.MVCWeb.Areas.Api.Controllers
{
    public class FetcherController : Controller
    {
        /// <summary>
        /// 获取指定剧集的下载地址
        /// </summary>
        /// <param name="albumId">专辑编号</param>
        /// <param name="episode">剧集</param>
        /// <param name="resolution">视频分辨率</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetFiles(int albumId, int episode, int resolution)
        {
            Conditions cons = new Conditions();
            cons.Add(new ConditionItem(LinkType.And, EpisodeInfo.AlbumIdField, CompareType.Equal, albumId));
            cons.Add(new ConditionItem(LinkType.And, EpisodeInfo.EpisodeIndexField, CompareType.Equal, episode));
            cons.Add(new ConditionItem(LinkType.And, EpisodeInfo.AllowDownloadField, CompareType.Equal, 1));

            EpisodeInfo info = EpisodeBLL.Find(cons, null);

            if (info != null)
            {
                SiteInfo site = SiteBLL.Find(info.SiteId);
                FetcherResult result = FetcherFactory.GetFiles(info.Url, (Resolution)resolution);
                result.Downloader = site.Downloader;
                SecurityResponseModel model = new SecurityResponseModel(result);
                return Json(model);
            }

            return Json(null);
        }

        /// <summary>
        /// 该方法通过传入的页面Url和分享Url获取系统中保存的剧集信息
        /// </summary>
        /// <param name="model">页面Url和分享Url组合的实体</param>
        /// <returns></returns>
        public ActionResult GetEpisode(FetcherModel model)
        {
            SecurityResponseModel responseModel = new SecurityResponseModel();

            return Json(responseModel, JsonRequestBehavior.AllowGet);
        }

        [ValidateInput(false)]
        public ActionResult CheckUrl(string url, string callback)
        {
            try
            {
                ChannelUrlInfo info = ChannelUrlBLL.GetTokenByUrl(url);

                if (string.IsNullOrEmpty(callback))
                {
                    return Json(new AjaxResultModel(info), JsonRequestBehavior.AllowGet);
                }
                else
                {
                    string json = JsonUtility.Json(new AjaxResultModel(info));

                    return Content("var callback = " + json);
                }
            }
            catch (Exception ex)
            {
                return Json(new AjaxResultModel(true, ex.Message), JsonRequestBehavior.AllowGet);
            }
        }
    }
}